package com.ruiyian.common.listener;

import com.ruiyian.common.constant.Constants;
import com.ruiyian.common.utils.spring.SpringUtils;
import com.ruiyian.project.predict.service.PredictService;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.*;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2022/1/1
 * Desc:
 */
@Slf4j
public class ResourceListener {
    private static ExecutorService fixedThreadPool = Executors.newCachedThreadPool();
    private WatchService ws;
    private String listenerPath;
    private static PredictService predictService;
    static {
        predictService = SpringUtils.getBean(PredictService.class);
    }
    private ResourceListener(String path) {
        try {
            ws = FileSystems.getDefault().newWatchService();
            this.listenerPath = path;
            start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void start() {
        fixedThreadPool.execute(new Listner(ws,this.listenerPath,predictService));
    }

    public static void addListener(String path) throws IOException {
        ResourceListener resourceListener = new ResourceListener(path);
        Path p = Paths.get(path);
        p.register(resourceListener.ws, StandardWatchEventKinds.ENTRY_MODIFY,
                StandardWatchEventKinds.ENTRY_DELETE,
                StandardWatchEventKinds.ENTRY_CREATE);
    }


  /*  public static void main(String[] args) throws IOException {
        ResourceListener.addListener("F:\\");
        ResourceListener.addListener("d:\\");
    }*/
}

class Listner implements Runnable {
    private WatchService service;
    private String rootPath;
    private PredictService predictService;


    public Listner(WatchService service, String rootPath, PredictService predictService) {
        this.service = service;
        this.rootPath = rootPath;
        this.predictService = predictService;
    }

    public void run() {
        try {
            while(true){
                WatchKey watchKey = service.take();
                List<WatchEvent<?>> watchEvents = watchKey.pollEvents();
                for(WatchEvent<?> event : watchEvents){
                    if(event.kind().name().equals("ENTRY_CREATE")){
                        String fileName = event.context().toString();
                        Thread.sleep(1000);
                        if(fileName.indexOf(Constants.SIMU)!= -1){
                            predictService.saveSimuMsg(rootPath+"/"+event.context());
                        }else if(fileName.indexOf(Constants.RECORD)!=-1){
                            predictService.saveRecordMsg(rootPath+"/"+event.context());
                        }
                    }
                    //TODO 根据事件类型采取不同的操作。。。。。。。
                    System.out.println("["+rootPath+"/"+event.context()+"]文件发生了["+event.kind()+"]事件"+    event.count());
                }
                watchKey.reset();
            }
        } catch (InterruptedException e) {

        }finally{
            System.out.println("fdsfsdf");
            try {
                service.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
